/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "BuildInfo.h"
#include "cJSON.h"
#include <bundle/native_interface_bundle.h>

REGISTER_PLUGIN_CLASS(BuildInfo)
bool BuildInfo::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    if(action == "init") {
        m_cbc = cbc;
        OH_NativeBundle_ApplicationInfo application = OH_NativeBundle_GetCurrentApplicationInfo();
        m_strBundleName = application.bundleName;
        executeArkTs("./PluginAction/GetAppInfo/GetAppInfo", 0, "getAppVersion", "BuildInfo", cbc);
    }
    return true;
}

bool BuildInfo::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(json != NULL && json->type == cJSON_Array) {
        int count = cJSON_GetArraySize(json);
        if(count > 0) {
            m_strAppName = cJSON_GetArrayItem(json, 0)->valuestring;
        } 
        
        if(count > 1) {
            m_strVersionNumber = cJSON_GetArrayItem(json, 1)->valuestring;
        } 
        
        if(count > 2) {
            m_lngVersionCode = atol(cJSON_GetArrayItem(json, 2)->valuestring);
        } 
        
        if(count > 3) {
            m_strModeName = cJSON_GetArrayItem(json, 3)->valuestring;
        }
        
        cJSON* pRet = cJSON_CreateObject();
        cJSON_AddStringToObject(pRet, "packageName", m_strBundleName.c_str());
        cJSON_AddStringToObject(pRet, "basePackageName", m_strBundleName.c_str());
        cJSON_AddStringToObject(pRet, "displayName", m_strAppName.c_str());
        cJSON_AddStringToObject(pRet, "name", m_strAppName.c_str());
        cJSON_AddStringToObject(pRet, "version", m_strVersionNumber.c_str());
        cJSON_AddNumberToObject(pRet, "versionCode", m_lngVersionCode);
        if(m_strModeName == "debug") {
            cJSON_AddTrueToObject(pRet, "debug");
        } else {
            cJSON_AddFalseToObject(pRet, "debug");
        }
        cJSON_AddStringToObject(pRet, "buildType", m_strModeName.c_str());
        //鸿蒙当前不支持
        cJSON_AddStringToObject(pRet, "flavor", "");
        //鸿蒙当前不支持
        cJSON_AddStringToObject(pRet, "buildDate", "");
        //鸿蒙当前不支持
        cJSON_AddStringToObject(pRet, "installDate", "");
        m_cbc.success(pRet);
        cJSON_Delete(pRet);
    }
    return true;
}